package com.fare.taluo.action.system.auth;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.apache.struts2.convention.annotation.Namespace;
import org.openkoala.auth.application.RoleApplication;
import org.openkoala.auth.application.vo.RoleVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.system.auth.UserInfoApplication;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.vo.auth.UserInfoVO;
import com.fare.util.JsonUtil;
/**
 * 用户信息相关
*/
@Controller("userAction")
@Scope("prototype")
@Namespace("/userAction")
public class UserAction extends BaseFdcAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Inject
	private RoleApplication roleApplication;
	
	@Resource(name ="userInfoApplication")
	private UserInfoApplication userInfoApplication;
	
	
	
	private List<RoleVO> roleList;
	
	private UserInfoVO userInfo;
	
	 private String status;
	 
	 private String oldEmail;
	 	 
	/**
	 * 进入个人信息中心
	 * */
	public String  inputUserInformationCenter(){
		if(status !=null){
			status=null;
		}
		Locale locale = super.getLocale();
		List<RoleVO> roles =roleApplication.findAllRole();
		roleList =new ArrayList<RoleVO>();
		roleList=this.interRole(roles, locale);
		userInfo=userInfoApplication.getCurrentUser();
		I18nUtil.UserSourceI18N(userInfo, locale);
		oldEmail=userInfo.getEmail();
		returnPageURL="/system/user/user_information_set.jsp";
		return "dispatcher";
	}
	/**
	 *修改个人信息
	*/
	public String updateUserInfo(){
		List<RoleVO> roles =roleApplication.findAllRole();
		roleList=new ArrayList<RoleVO>();
		Locale locale = super.getLocale();
		roleList=this.interRole(roles, locale);
		if(!oldEmail.equals(userInfo.getEmail())){
			if(checkEmailExceptCurrent()){
				status = "emailexcept";
				returnPageURL="/system/user/user_information_set.jsp";
				return "dispatcher";
			}
		}
		
		try {
			userInfoApplication.updateUser(userInfo);
			status = "success";
		} catch (Exception e) {
			e.printStackTrace();
			status = "error";
		}
		returnPageURL="/system/user/user_information_set.jsp";
		return "dispatcher";
	}
	
	/**
	 * 检查用户名是否存在，当前用户除外
	 * */
	private boolean checkEmailExceptCurrent(){
		boolean bool = false;
		try {
			bool=userInfoApplication.queryIsExist(userInfo.getUserId(), userInfo.getEmail(), null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * 角色国际化
	 * 
	*/
	public List<RoleVO> interRole(List<RoleVO> roles,Locale locale ){
		List<RoleVO> roleLists=new ArrayList<RoleVO>();
		if(roles.size()>0){
			for (RoleVO vo : roles) {
				if ("收益管理员".equals(vo.getName())) {
					if(locale.toString().equalsIgnoreCase("en_US")){
						vo.setName("RevenueManager");
					}
					roleLists.add(vo);
				}else if ("渠道管理员".equals(vo.getName())) {
					if(locale.toString().equalsIgnoreCase("en_US")){
						vo.setName("ChannelManager");
					}
					roleLists.add(vo);
				}

			}
		}
		
		return roleLists;
	}
	
	//用户密码修改
	public String updatePassword(){
		try {
			Boolean flag=userInfoApplication.updatePassWord(userInfo.getAccoutId(),userInfo.getPassword() );
			if(flag==true){
				status = "success";
			}else{
				status = "false";
			}
		} catch (Exception e) {
			status = "error";
			e.printStackTrace();
		}
		returnPageURL="/system/user/password_set.jsp";
		return "dispatcher";
	}
	
	
	/**
	 * 检查密码是否正确
	 * */
	public void checkPassword(){
		if(userInfo == null || !userInfo.getLoginId().equals("") || userInfo.getPassword() !=null ){
			JsonUtil.toJSONHtmlOut("false");
		}
		boolean bool = userInfoApplication.checkInputPassword(userInfo.getUserId(), userInfo.getOldpassword());
		
		if(bool){
			JsonUtil.toJSONHtmlOut("true");
		}else{
			JsonUtil.toJSONHtmlOut("false");
		}
	//	MyMD5Util.validPassword(null,null);
	}
	
	public String setPasswordInput(){
		returnPageURL="/system/user/password_set.jsp";
		return "dispatcher";
	}
	
	public UserInfoVO getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfoVO userInfo) {
		this.userInfo = userInfo;
	}
	public List<RoleVO> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<RoleVO> roleList) {
		this.roleList = roleList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	public String getOldEmail() {
		return oldEmail;
	}
	public void setOldEmail(String oldEmail) {
		this.oldEmail = oldEmail;
	}
	
	

}
